window.onload = initPage;


function initPage(){
    
    var fields = getElements("input","");
        for(var i=0;i<fields.length;i++){
        var field = fields[i];
        if(field.type == "text" || field.type == "password"){
            addEventHandler(field,"keyup",checkFieldStandard);
            addEventHandler(field,"load",checkFieldStandard);
         
        }
        if(field.type == "submit"){
            field.disabled = true;
            field.onmouseover = alert;
        }
    }
}

function checkSubmitPermission()
{
       
    var thereIsDenied = false;
    var fields = document.getElementsByTagName("input");
    
    for(var i=0;i<fields.length;i++){
        var field = fields[i];
        if(field.type == "text"){
            if(field.className == "denied" || field.className == ""){
                thereIsDenied = true;
                break;
            }
        }
    }
   
   var submit;
   var submits = getElements("input","submit");

    if(thereIsDenied){
        for(var e=0;e<submits.length;e++){
           submit = submits[e];
           submit.disabled = true;          
        } 
    }else{
        for(var o=0;o<submits.length;o++){
            submit = submits[o];
            submit.disabled = false;
        } 
    }
    
}

function checkFieldStandard(e){
    var obj = getActivatedObject();
   

   if(obj.value.length >= 3){
        obj.className = "approved";      
    }else{
            obj.className = "denied";
    }
    
/*############################################
                HERE GOES THE EXCEPTIONS     
*/
    switch(obj.id){
        case "telefone":
            if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }           
            break;
        case "cpfField":
           if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }           
            break;
        case "ddd":
           if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }           
            break;
        case "passaporteField":
           if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }           
            break;
       case "numero":
           if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }     
            break;
       case "confirma":                      
           if(obj.value == document.getElementById("senha").value){
                obj.className = "approved";
                
            }else{
                obj.className = "denied";
            }
            break;
        case "hora":                      
           if(jQueryObjIsValid(obj)){
               obj.className = "approved";
           }else
           {
                obj.className = "denied";
           }     
            break;
    }
                
            
    
/*############################################
                END OF EXCEPTIONS     
*/
     checkSubmitPermission();
    return;
}
